package com.xbongbong.workorder.abstracts;/*/*/

import com.xbongbong.paas.domain.entity.ext.OptionalRangeEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.field.LinkedAttrPoJo;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.saas.domain.entity.WorkOrderExplainEntity;
import com.xbongbong.saas.domain.entity.ext.WorkOrderFormEntityExt;
import com.xbongbong.sys.domain.entity.DepartmentEntity;
import com.xbongbong.sys.domain.entity.RoleEntity;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 处理字段回显用户，角色，部门的问题
 * @author 魏荣杰
 * @date 2019/4/8 21:47
 * @since v1.0
 * @version v1.0
 */
@Component
public abstract class AbstractWorkOrderFromExplainHandlerName {

    private static final Logger LOG = LoggerFactory.getLogger(AbstractWorkOrderFromExplainHandlerName.class);

    @Resource
    private FormDataFormExplainHelper formDataFormExplainHelper;

    /**
     * Description:
     * @param explainList
     * @param workOrderFormEntityExt
     * @param param
     * @return com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO
     * @throws XbbException XbbException
     * @author 魏荣杰
     * @date 2019/4/11 10:54
     * @since v1.0
     */
    public abstract HandlerExplainVO handlerExplain(List<FieldAttrEntity> explainList, WorkOrderFormEntityExt workOrderFormEntityExt, Map<String, Object> param, HandlerExplainDTO handlerExplainDTO) throws XbbException;


    /**
     * Description: 设置子表单字段属性
     * @param entity
     * @param param
     * @param userMap
     * @param departmentMap
     * @param roleMap
     * @param formExplainMap
     * @return FieldAttrEntity
     * @throws XbbException XbbException
     * @author 魏荣杰
     * @date 2019/4/11 15:32
     * @since v1.0
     */
    public FieldAttrEntity setSubFieldAttrEntity(FieldAttrEntity entity, Map<String, Object> param, Map<String, UserEntity> userMap,
                                                 Map<String, DepartmentEntity> departmentMap, Map<String, RoleEntity> roleMap,
                                                 Map<String, WorkOrderExplainEntity> formExplainMap, HandlerExplainDTO handlerExplainDTO) throws XbbException {
        List<? extends FieldAttrEntity> subExplainList = entity.getSubForm().getItems();
        List<FieldAttrEntity> subFieldAttrList = new ArrayList<>();
        try {
            if (subExplainList == null) {
                entity.getSubForm().setItems(new ArrayList<>());
                return entity;
            }
            Iterator<? extends FieldAttrEntity> iterator = subExplainList.iterator();
            while (iterator.hasNext()){
                FieldAttrEntity subFieldAttrEntity = iterator.next();
                LinkInfoPojo subLinkInfo = subFieldAttrEntity.getLinkInfo();
                if (Objects.equals(subFieldAttrEntity.getVisible(), 0)) {
                    iterator.remove();
                    continue;
                }
                // 高级可见规则回显用户名称
                if (ExplainUtil.invisibleOfScope(subFieldAttrEntity, handlerExplainDTO)) {
                    iterator.remove();
                    continue;
                }
                if(subLinkInfo != null) {
                    Long linkFormId = subLinkInfo.getLinkFormId();
                    if (Objects.isNull(linkFormId)) {
                        continue;
                    }
                    WorkOrderExplainEntity workOrderExplainEntity = formExplainMap.get(linkFormId.toString());
                    if (workOrderExplainEntity == null) {
                        continue;
                    }
                }
                subFieldAttrEntity = setFieldAttrEntity(subFieldAttrEntity, handlerExplainDTO, userMap, departmentMap, roleMap, formExplainMap);
                subFieldAttrList.add(subFieldAttrEntity);
            }
            entity.getSubForm().setItems(subExplainList);
        } catch (Exception e) {
            LOG.error("AbstractWorkOrderFromExplainHandlerName.setSubFieldAttrEntity: ", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return entity;
    }

    /**
     * 设置字段的属性，回显用户名称，部门名称，角色名称，已经关联表的字段信息
     * @param entity 字段对象
     * @param handlerExplainDTO 查询条件集合
     * @param userMap 用户容器
     * @param departmentMap 部门容器
     * @param roleMap 角色容器
     * @param formExplainMap 当前应用内表单字段容器
     * @return 返回填充的字段信息
     * @throws XbbException 抛出业务异常
     */
    public FieldAttrEntity setFieldAttrEntity(FieldAttrEntity entity, HandlerExplainDTO handlerExplainDTO, Map<String, UserEntity> userMap, Map<String, DepartmentEntity> departmentMap, Map<String, RoleEntity> roleMap, Map<String, WorkOrderExplainEntity> formExplainMap) throws XbbException {
        try {
            if (Objects.nonNull(entity.getDefaultAttr())) {
                List<OptionalRangeEntity> defaultList = entity.getDefaultAttr().getDefaultList();
                if(defaultList != null && !defaultList.isEmpty()){
                    defaultList = formDataFormExplainHelper.handleOptionalRangeList(defaultList,handlerExplainDTO.getCorpid(), userMap, departmentMap, roleMap);
                    entity.getDefaultAttr().setDefaultList(defaultList);
                }
            }
            LinkInfoPojo linkInfoPojo = entity.getLinkInfo();
            if(linkInfoPojo != null){
                Long linkFormId = linkInfoPojo.getLinkFormId();
                if (Objects.isNull(linkFormId)) {
                    // 返回没有配置关联表的关联数据和关联查询的字段
                    return entity;
                }
                List<LinkedAttrPoJo> linkedAttrList = new ArrayList<>();
                WorkOrderExplainEntity workOrderExplainEntity = formExplainMap.get(linkFormId.toString());
                if (Objects.isNull(workOrderExplainEntity)) {
                    // 返回关联表已被删除关联数据和关联查询的字段
                    return entity;
                }
                Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMap(workOrderExplainEntity.getExplains(), null);
                for (LinkedAttrPoJo item : linkInfoPojo.getLinkedAttr()) {
                    FieldAttrEntity fieldAttrEntity = explainMap.get(item.getAttr());
                    if(fieldAttrEntity == null){
                        continue;
                    }
                    if (FieldTypeEnum.SUB_FORM.getType().equals(item.getFieldType())) {
                        item.setSubForm(fieldAttrEntity.getSubForm());
                    }
                    item.setAttrName(fieldAttrEntity.getAttrName());
                    linkedAttrList.add(item);
                }
                linkInfoPojo.setLinkedAttr(linkedAttrList);
            }
        }catch (Exception e){
            LOG.error("AbstractWorkOrderFromExplainHandlerName.setFieldAttrEntity：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return entity;
    }

}
